<template>
  <div class="book-container">
    <!-- 书的左侧 -->
    <div class="book-left">
      <div class="book-cover">
        <div class="banner-container" :class="{ 'fade-out': isFading, 'fade-in': !isFading }">
          <div class="logo-container">
            <img :src="currentBanner.image" alt="{{ currentBanner.alt }}" class="logo" />
          </div>
          <div class="quote-container">
            <p class="happy-quote">{{ currentBanner.quote }}</p>
            <p class="quote-author">— {{ currentBanner.author }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 书的右侧 - 登录表单 -->
    <div class="book-right">
      <div class="login-form">
        <h3 class="form-title">用户登录</h3>
        <el-form ref="loginFormRef" :model="loginDTO" :rules="rules" class="el-form">
          <el-form-item prop="username">
            <el-input v-model="loginDTO.username" placeholder="请输入用户名" prefix-icon="User"
              :prefix-icon-style="{ color: '#3a86ff' }" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginDTO.password" type="password" placeholder="请输入密码" prefix-icon="Lock"
              show-password />
          </el-form-item>
          <el-form-item prop="verifyCode">
            <div class="captcha-container">
              <el-input v-model="loginDTO.verifyCode" placeholder="请输入验证码" style="width: 60%;" />
              <div class="captcha-image-container">
                <img :src="verifyCodeImg" alt="验证码" class="captcha-image" @click="generateCaptcha" />
              </div>
            </div>
          </el-form-item>
          <el-form-item class="form-options">
            <el-checkbox v-model="remember">记住我</el-checkbox>
            <el-link type="primary" :underline="false" @click="handleForgotPassword">忘记密码?</el-link>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" round style="width: 100%; background-color: #3a86ff; border-color: #3a86ff;"
              @click="handleLogin" :loading="loading">
              登录
            </el-button>
          </el-form-item>
          <div class="third-party-login">
            <span class="login-separator">其他登录方式</span>
            <div class="login-icons">

            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { AdminLoginDTO } from '@/api/acl/user/type'
import { onMounted, reactive, ref, toRefs, computed } from 'vue'
import type { FormInstance } from 'element-plus'
import useUserStore from '@/store/modules/user'
import { login } from '@/api/acl/user'
import { useRoute, useRouter } from 'vue-router'
import { ElNotification, ElMessage } from 'element-plus'
import { create } from '@/api/acl/captcha'

const loginState = reactive({
  loginDTO: {
    username: '',
    password: '',
    verifyCode: '',
    uuid: '',
  } as AdminLoginDTO,
  remember: false,
  loading: false,
  rules: {
    username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
    verifyCode: [{ required: true, message: '验证码不能为空', trigger: 'blur' }],
  },
  socials: ['wechat', 'QQ', 'weibo', 'github', 'gitee', 'meta', 'zfb'],
  verifyCodeImg: '',
})


const { loginDTO, remember, rules, loading, verifyCodeImg } = toRefs(loginState)
const ruleFormRef = ref<FormInstance>()

const userStore = useUserStore()
const $route = useRoute()
const $router = useRouter()
// 定义banner数据 - 图片和名人名言的组合
const banners = [
  {
    image: '/src/assets/images/xdw.gif',
    alt: '奋斗动画图',
    quote: '千里之行，始于足下。',
    author: '老子'
  },
  {
    image: '/src/assets/images/flower.gif',
    alt: '花朵动画图',
    quote: '不积跬步，无以至千里。',
    author: '荀子'
  },
  {
    image: '/src/assets/images/chinese_fu.gif',
    alt: '福字动画图',
    quote: '淡泊明志，宁静致远。',
    author: '诸葛亮'
  },
  {
    image: '/src/assets/images/dilivery.gif',
    alt: '交付动画图',
    quote: '精诚所至，金石为开。',
    author: '庄子'
  },
  {
    image: '/src/assets/images/lattern.gif',
    alt: '灯笼动画图',
    quote: '穷且益坚，不坠青云之志。',
    author: '王勃'
  }
];

// 当前显示的banner索引
const currentBannerIndex = ref(Math.floor(Math.random() * banners.length));
// 当前显示的banner
const currentBanner = computed(() => banners[currentBannerIndex.value]);
// 动画状态
const isFading = ref(false);

// 定时切换banner
setInterval(() => {
  isFading.value = true;
  // 等待动画结束后切换banner
  setTimeout(() => {
    currentBannerIndex.value = (currentBannerIndex.value + 1) % banners.length;
    isFading.value = false;
  }, 500);
}, 5000);



// 生成新的验证码
const generateCaptcha = async () => {
  const result = await create()
  loginDTO.value.uuid = result.data.id
  verifyCodeImg.value = result.data.url
}


// 处理登录
const handleLogin = async () => {
  try {
    await ruleFormRef.value?.validate()
    loading.value = true
    const result = await login(loginDTO.value)
    await userStore.cacheAccessToken(result.data)
    const redirect: any = $route.query.redirect
    await $router.push({ path: redirect || '/' })
    loading.value = false
    ElNotification({
      type: 'success',
      message: '登录成功,欢迎回来',
    })
  } catch (error) {
    loading.value = false
  }
}

// 处理忘记密码
const handleForgotPassword = () => {
  ElMessage.info('忘记密码功能开发中');
};

// 初始化验证码和检查记住的用户名
onMounted(() => {
  generateCaptcha();
  const savedUsername = localStorage.getItem('username');
  if (savedUsername) {
    // loginForm.username = savedUsername;
    // loginForm.rememberMe = true;
  }
});
</script>

<style scoped>
.book-container {
  display: flex;
  min-height: 100vh;
  background-color: #f5f5f5;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

/* 书的左侧样式 */
.book-left {
  width: 400px;
  height: 500px;
  background-color: #3a86ff;
  border-radius: 10px 0 0 10px;
  position: relative;
  overflow: hidden;
  box-shadow: -5px 0 15px rgba(0, 0, 0, 0.1);
  transform-origin: right top;
  transition: transform 0.5s ease;
}

.book-left:hover {
  transform: perspective(1000px) rotateY(5deg);
}

.book-cover {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  padding: 40px;
  box-sizing: border-box;
}

.banner-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
  box-sizing: border-box;
  transition: opacity 0.7s cubic-bezier(0.4, 0, 0.2, 1), transform 0.7s cubic-bezier(0.4, 0, 0.2, 1);
}

.logo-container {
  margin-bottom: 30px;
  width: 100%;
  display: flex;
  justify-content: center;
}

.logo {
  width: 180px;
  height: 180px;
  max-width: 100%;
  object-fit: contain;
}

.book-title {
  font-size: 36px;
  margin-bottom: 15px;
  font-weight: bold;
  background: linear-gradient(45deg, #ffffff, #a6c1ee);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  letter-spacing: 2px;
  font-family: 'Arial', 'Microsoft YaHei', sans-serif;
  animation: titleShine 3s infinite alternate;
}

@keyframes titleShine {
  0% {
    background-position: 0% 50%;
  }

  100% {
    background-position: 100% 50%;
  }
}

.book-description {
  font-size: 16px;
  text-align: center;
  margin-bottom: 20px;
}

.quote-container {
  text-align: center;
  max-width: 280px;
}

.happy-quote {
  font-size: 18px;
  font-style: italic;
  line-height: 1.5;
  color: #fff299;
  /* 醒目的浅黄色 */
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  margin-bottom: 10px;
}

.quote-author {
  font-size: 14px;
  color: #ffffff;
  opacity: 0.8;
  font-weight: 500;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}

.happy-quote::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 2px;
  background: linear-gradient(90deg, transparent, #fff299, transparent);
}

.happy-quote:hover {
  transform: scale(1.05);
  color: #fff7c0;
  transition: transform 0.3s ease, color 0.3s ease;
}

.fade-in {
  opacity: 1;
  transform: translateY(0);
}

.fade-out {
  opacity: 0;
  transform: translateY(10px);
}

.happy-quote:hover {
  transform: scale(1.05);
  transition: transform 0.3s ease;
}



/* 气泡装饰 */
.bubble {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.3);
  animation: float 8s infinite ease-in-out;
  box-shadow: 0 0 15px rgba(255, 255, 255, 0.5);
}

@keyframes float {

  0%,
  100% {
    transform: translateY(0) scale(1);
    opacity: 0.3;
  }

  50% {
    transform: translateY(-20px) scale(1.1);
    opacity: 0.5;
  }
}

/* 添加一些随机位置的气泡 */
.decoration .bubble:nth-child(1) {
  width: 30px;
  height: 30px;
  left: 10%;
  top: 20%;
  animation-delay: 0s;
}

.decoration .bubble:nth-child(2) {
  width: 20px;
  height: 20px;
  left: 30%;
  top: 70%;
  animation-delay: 1s;
}

.decoration .bubble:nth-child(3) {
  width: 40px;
  height: 40px;
  left: 60%;
  top: 30%;
  animation-delay: 2s;
}

.decoration .bubble:nth-child(4) {
  width: 25px;
  height: 25px;
  left: 80%;
  top: 60%;
  animation-delay: 3s;
}

/* 书的右侧样式 */
.book-right {
  width: 400px;
  height: 500px;
  background-color: white;
  border-radius: 0 10px 10px 0;
  box-shadow: 5px 0 15px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  z-index: 2;
}

.login-form {
  width: 80%;
}

.form-title {
  font-size: 24px;
  margin-bottom: 30px;
  text-align: center;
  color: #333;
  font-weight: bold;
}

/* Element Plus 表单样式调整 */
.el-form {
  width: 100%;
}

.el-form-item {
  margin-bottom: 20px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-checkbox__label {
  font-size: 14px;
  color: #555;
}

.el-link {
  font-size: 14px;
  color: #3a86ff;
}

.el-link:hover {
  color: #2973e6;
}

/* 验证码样式 */
.captcha-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.captcha-image-container {
  display: flex;
  align-items: center;
  width: 38%;
}

.captcha-image {
  width: 100%;
  height: 40px;
  border-radius: 4px;
  cursor: pointer;
  margin-right: 5px;
}

.refresh-icon {
  color: #3a86ff;
  cursor: pointer;
  font-size: 18px;
}

/* 第三方登录样式 */
.third-party-login {
  margin-top: 20px;
  text-align: center;
}

.login-separator {
  display: inline-block;
  margin-bottom: 15px;
  color: #999;
  font-size: 14px;
  position: relative;
}

.login-separator::before,
.login-separator::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 60px;
  height: 1px;
  background-color: #e0e0e0;
}

.login-separator::before {
  left: -70px;
}

.login-separator::after {
  right: -70px;
}

.login-icons {
  display: flex;
  justify-content: center;
  gap: 30px;
}

.login-icon {
  font-size: 24px;
  cursor: pointer;
  transition: color 0.3s;
}

.login-icon:nth-child(1) {
  color: #1da1f2;
}

.login-icon:nth-child(2) {
  color: #07c160;
}

.login-icon:hover {
  opacity: 0.8;
}

.login-button:hover {
  background-color: #359169;
}

/* 为登录按钮添加脉冲效果 */
.el-button--primary {
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(58, 134, 255, 0.7);
  }

  70% {
    box-shadow: 0 0 0 10px rgba(58, 134, 255, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(58, 134, 255, 0);
  }
}

/* 增强装饰元素 */
.decoration {
  position: relative;
  overflow: hidden;
}

.decoration::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  animation: shine 5s infinite;
}

@keyframes shine {
  100% {
    left: 100%;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .book-container {
    flex-direction: column;
  }

  .book-left {
    width: 100%;
    height: 200px;
    border-radius: 10px 10px 0 0;
    transform: none;
  }

  .book-right {
    width: 100%;
    height: auto;
    min-height: 350px;
    border-radius: 0 0 10px 10px;
    padding: 20px 0;
  }

  .book-cover {
    padding: 20px;
  }

  .logo-container {
    margin-bottom: 10px;
  }

  .logo {
    width: 50px;
    height: 50px;
  }

  .book-title {
    font-size: 20px;
  }

  .book-description {
    margin-bottom: 10px;
  }

  .decoration {
    display: none;
  }
}
</style>